import java.util.*;

/**
 * @author LKQ
 * @date 2022/4/17 15:42
 * @description 双指针模拟
 */
public class Solution {
    public static void main(String[] args) {
        Solution solution = new Solution();
        solution.digitSum("12346", 2);
    }
    public String digitSum(String s, int k) {
        while (s.length() > k) {
            String ans = "";
            for (int l = 0, r = 0; r < s.length(); r++) {
                if (r - l + 1 == k) {
                    ans += help(s.substring(l, r + 1));
                    l = r + 1;
                }
                if (l != s.length() && r == s.length() - 1) {
                    // 最后不足一个k长度
                    ans += help(s.substring(l));
                }
            }
            s = ans;
        }
        return s;
    }
    public String help(String s) {
        int ans = 0;
        for (int i = 0; i < s.length(); i++) {
            ans += s.charAt(i) - '0';
        }
        return String.valueOf(ans);
    }
}
